﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace _4591BRD
{
    class NameCacheManager
    {
        private static NameCacheManager _instance = null;
        public static NameCacheManager Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new NameCacheManager();
                return _instance;
            }
        }

        public Dictionary<int, string> TankList;
        public Dictionary<int, string> PlaneList;
        public Dictionary<int, string> WeaponList;
        public Dictionary<int, string> EquipList;

        private Dictionary<int, NameCache> _CharacterCache;
        private Dictionary<int, NameCache> _TeamCache;

        private void InitVehicleList()
        {
            TankList = new Dictionary<int, string>();
            TankList.Add(4016, "穿梭者");
            TankList.Add(4020, "流星");
            TankList.Add(4001, "豚鼠");
            TankList.Add(4013, "猫鼬");
            TankList.Add(4002, "剑齿虎");
            TankList.Add(4004, "能量车");
            TankList.Add(4005, "卫星车");
            TankList.Add(4006, "重生车");
            TankList.Add(4011, "苍狼");
            TankList.Add(4012, "游骑兵");
            TankList.Add(4014, "霹雳");
            TankList.Add(4018, "虎鲨");
            TankList.Add(4021, "清道夫");
            TankList.Add(4022, "地震");
            TankList.Add(4017, "猛犸");
            TankList.Add(4009, "沙暴");

            PlaneList = new Dictionary<int, string>();
            PlaneList.Add(4003, "飓风");
            PlaneList.Add(4007, "云枭");
            PlaneList.Add(4008, "游隼");
            PlaneList.Add(4019, "战争号角");
            PlaneList.Add(4015, "季风");
            PlaneList.Add(4010, "死亡之翼");
        }

        private void InitWeaponList()
        {
            WeaponList = new Dictionary<int, string>();
            WeaponList.Add(0, "未知武器");

            EquipList = new Dictionary<int, string>();
            EquipList.Add(0, "未知装备");
        }

        public NameCacheManager()
        {
            InitVehicleList();
            InitWeaponList();

            _CharacterCache = new Dictionary<int, NameCache>();
            _TeamCache = new Dictionary<int, NameCache>();

            string sql = "";
            SqlDataReader reader;

            //try
            //{
            //    //角色
            //    sql = " SELECT * FROM CharacterBase WITH(NOLOCK) WHERE CharacterState=1";
            //    reader = SqlHelper.ExecuteReader(Common.ConnStrTY, CommandType.Text, sql, null);
            //    while (reader.Read())
            //    {
            //        int charid = int.Parse(reader["CharacterID"].ToString());

            //        NameCache theObj = new NameCache();
            //        theObj.CharID = charid;
            //        theObj.CharName = reader["CharacterName"].ToString();
            //        theObj.CountryID = int.Parse(reader["CountryID"].ToString());
            //        theObj.TeamID = int.Parse(reader["TeamID"].ToString());

            //        _CharacterCache.Add(charid, theObj);
            //    }
            //    reader.Close();

            //    //军团
            //    sql = " SELECT * FROM Team WITH(NOLOCK) WHERE TeamState=1";
            //    reader = SqlHelper.ExecuteReader(Common.ConnStrTY, CommandType.Text, sql, null);
            //    while (reader.Read())
            //    {
            //        int teamid = int.Parse(reader["TeamID"].ToString());

            //        NameCache theObj = new NameCache();
            //        theObj.TeamID = teamid;
            //        theObj.TeamName = reader["TeamName"].ToString();
            //        theObj.CountryID = int.Parse(reader["CorpsID"].ToString());
            //        theObj.ShortName = reader["TeamShortName"].ToString();

            //        _TeamCache.Add(teamid, theObj);
            //    }
            //    reader.Close();
            //}
            //catch (SqlException se)
            //{
            //    Log.WriteError(se.Message);
            //}
            //catch (Exception e)
            //{
            //    Log.WriteError(e.Message);
            //}
        }

        public NameCache GetCharByID(int charid)
        {
            NameCache theObj;
            if (!_CharacterCache.ContainsKey(charid))
            {
                theObj = new NameCache();
                //try
                //{
                //    string sql = " SELECT * FROM CharacterBase WITH(NOLOCK) WHERE CharacterID=" + charid.ToString();
                //    SqlDataReader reader = SqlHelper.ExecuteReader(Common.ConnStrTY, CommandType.Text, sql, null);
                //    if (reader.Read())
                //    {
                //        theObj.CharID = charid;
                //        theObj.CharName = reader["CharacterName"].ToString();
                //        theObj.CountryID = int.Parse(reader["CountryID"].ToString());
                //        theObj.TeamID = int.Parse(reader["TeamID"].ToString());

                //        _CharacterCache.Add(charid, theObj);
                //    }
                //    reader.Close();
                //}
                //catch (SqlException se)
                //{
                //    Log.WriteError(se.Message);
                //}
                //catch (Exception e)
                //{
                //    Log.WriteError(e.Message);
                //}
            }
            else
                theObj = _CharacterCache[charid];

            return theObj;
        }

        public NameCache GetTeamByID(int teamid)
        {
            NameCache theObj;
            if (!_TeamCache.ContainsKey(teamid))
            {
                theObj = new NameCache();
                //try
                //{
                //    string sql = " SELECT * FROM Team WITH(NOLOCK) WHERE TeamID=" + teamid.ToString();
                //    SqlDataReader reader = SqlHelper.ExecuteReader(Common.ConnStrTY, CommandType.Text, sql, null);
                //    if (reader.Read())
                //    {
                //        theObj.TeamID = teamid;
                //        theObj.TeamName = reader["TeamName"].ToString();
                //        theObj.CountryID = int.Parse(reader["CorpsID"].ToString());                        
                //        theObj.ShortName = reader["TeamShortName"].ToString();

                //        _TeamCache.Add(teamid, theObj);
                //    }
                //    reader.Close();
                //}
                //catch (SqlException se)
                //{
                //    Log.WriteError(se.Message);
                //}
                //catch (Exception e)
                //{
                //    Log.WriteError(e.Message);
                //}
            }
            else
                theObj = _TeamCache[teamid];

            return theObj;
        }

        public string GetCharNameByID(int charid)
        {
            string strRet = "";
            NameCache theObj = GetCharByID(charid);
            if (theObj != null)
                strRet = theObj.CharName;

            return strRet;
        }

        public int GetCountryByCharID(int charid)
        {
            int iRet = 0;
            NameCache theObj = GetCharByID(charid);
            if (theObj != null)
                iRet = theObj.CountryID;

            return iRet;
        }

        public int GetTeamIDByCharID(int charid)
        {
            int teamid = 0;

            NameCache theObj = GetCharByID(charid);
            if (theObj != null)
                teamid = theObj.TeamID;

            return teamid;
        }

        public string GetTeamShortNameByCharID(int charid)
        {
            string strRet = "";
            int teamid = 0;

            NameCache theObj = GetCharByID(charid);
            if (theObj != null)
                teamid = theObj.TeamID;

            if (teamid > 0)
            {
                theObj = GetTeamByID(teamid);
                strRet = theObj.ShortName;
            }

            return strRet;
        }
    }
}
